package packing;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;


public class Reader {

	public static Vector<Integer> idVector = new Vector<Integer>();
	
	public static List<State> readBRSet (String filepath){
		List<State> problemList = new ArrayList<State>();
		try {
			 // Open the file that is the first 
		    // command line parameter
			File file = new File(filepath);
		    FileInputStream fstream = new FileInputStream(file);
		    String instanceNumber = file.getName();
		    instanceNumber= instanceNumber.replaceAll("br", "");
		    instanceNumber = instanceNumber.replaceAll(".txt", "");
		    
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    strLine = br.readLine();
		    int totalInstance = Integer.parseInt(strLine.trim());
		   
		    for (int i = 0; i < totalInstance; i++) {
				strLine = br.readLine().trim();
				String [] strArray = strLine.split(" ");
				String problemID= instanceNumber + "/"+ strArray[0];
				strArray = br.readLine().trim().split(" ");
				Container container = new Container(Integer.parseInt(strArray[1]), 
						Integer.parseInt(strArray[0]), Integer.parseInt(strArray[2]), Integer.parseInt(strArray[3]));
				System.out.println("ขนาด Container : "+container.toString());
				int boxtypeNumber = Integer.parseInt(br.readLine().trim());
				List<BoxInput> boxInputList = new ArrayList<BoxInput>();
				System.out.println("\nมีจำนวนกล่องทั้งสิ้น : "+boxtypeNumber+" กล่อง ดังนี้...");
				System.out.println("======================================================================");
				for (int j = 0; j < boxtypeNumber; j++) {
					strArray = br.readLine().trim().split(" ");
					int id = Integer.parseInt(strArray[0]);
					idVector.add(id);
					int length = Integer.parseInt(strArray[1]);
					int weight = Integer.parseInt(strArray[8]);
					int pressure = Integer.parseInt(strArray[9]);
					boolean yRotation = false;
					if(Integer.parseInt(strArray[2]) ==1)yRotation = true;
					
					int width = Integer.parseInt(strArray[3]);
					boolean xRotation = false;
					if(Integer.parseInt(strArray[4]) ==1)xRotation = true;
					
					int height = Integer.parseInt(strArray[5]);
					boolean zRotation = false;
					if(Integer.parseInt(strArray[6]) ==1)zRotation = true;
					int quantity = Integer.parseInt(strArray[7]);
					BoxInput boxinput = new BoxInput(id, width, xRotation, length, yRotation, height, zRotation, quantity , weight, pressure);
					boxInputList.add(boxinput);
					System.out.println(boxinput.toString());
				}
				
				problemList.add(new State(problemID, container, boxInputList));
				
			}
		    in.close();
		   
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		return problemList;
		
	}
}
